package com.quectel.core.module.partybuilding.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.partybuilding.dao.PartyPersonBirthdayMsgRecordDao;
import com.quectel.core.module.partybuilding.dto.PartyOrganizationDto;
import com.quectel.core.module.partybuilding.dto.PartyPersonBirthdayMsgRecordDto;
import com.quectel.core.module.partybuilding.dto.PartyPersonDto;
import com.quectel.core.module.partybuilding.entity.PartyPersonBirthdayMsgRecordEntity;
import com.quectel.core.module.partybuilding.service.PartyOrganizationService;
import com.quectel.core.module.partybuilding.service.PartyPersonBirthdayMsgRecordService;
import com.quectel.core.module.partybuilding.service.PartyPersonService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:17
 */
@DubboService
public class PartyPersonBirthdayMsgRecordServiceImpl implements PartyPersonBirthdayMsgRecordService{

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyPersonBirthdayMsgRecordServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARTY_PERSON_BIRTHDAY_MSG_RECORD_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARTY_PERSON_BIRTHDAY_MSG_RECORD_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PartyPersonBirthdayMsgRecordDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARTY_PERSON_BIRTHDAY_MSG_RECORD_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PartyPersonBirthdayMsgRecordDao partyPersonBirthdayMsgRecordDao;
    @Autowired
    private PartyOrganizationService partyOrganizationService;
    @Autowired
    private PartyPersonService partyPersonService;


    @Override
    public PartyPersonBirthdayMsgRecordDto selectOne(Long personId, String currentYear) {
        PartyPersonBirthdayMsgRecordEntity recordEntity = partyPersonBirthdayMsgRecordDao.selectOne(new LambdaQueryWrapper<PartyPersonBirthdayMsgRecordEntity>()
                .eq(PartyPersonBirthdayMsgRecordEntity::getPersonId, personId)
                .eq(PartyPersonBirthdayMsgRecordEntity::getYear, currentYear)
                .last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(recordEntity, PartyPersonBirthdayMsgRecordDto.class);
    }

    @Override
    public PartyPersonBirthdayMsgRecordDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARTY_PERSON_BIRTHDAY_MSG_RECORD_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PartyPersonBirthdayMsgRecordDto selectById(Long id){
            PartyPersonBirthdayMsgRecordEntity entity = partyPersonBirthdayMsgRecordDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,PartyPersonBirthdayMsgRecordDto.class));
    }

    @Override
    public List<PartyPersonBirthdayMsgRecordDto> queryList(Map<String,Object> params){
        List<PartyPersonBirthdayMsgRecordEntity> list = partyPersonBirthdayMsgRecordDao.queryList(params);
        List<PartyPersonBirthdayMsgRecordDto> result =  CopyUtils.copyList(list,PartyPersonBirthdayMsgRecordDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PartyPersonBirthdayMsgRecordDto paddingField(PartyPersonBirthdayMsgRecordDto dto){
        if (dto != null){
            if (dto.getPartyId()!=null){
                PartyOrganizationDto organizationDto = partyOrganizationService.selectCacheById(dto.getPartyId());
                if (organizationDto !=null){
                    dto.setPartyName(organizationDto.getName());
                }
            }
            if (dto.getPersonId()!=null){
                PartyPersonDto partyPersonDto = partyPersonService.selectCacheById(dto.getPersonId());
                if (partyPersonDto != null){
                    dto.setPartyPerson(partyPersonDto);
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return partyPersonBirthdayMsgRecordDao.queryTotal(params);
    }

    @Override
    public Long save(PartyPersonBirthdayMsgRecordDto dto){
        PartyPersonBirthdayMsgRecordEntity entity = CopyUtils.copyObj(dto,PartyPersonBirthdayMsgRecordEntity.class);
        entity.setId(Snowflake.nextId());
        partyPersonBirthdayMsgRecordDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PartyPersonBirthdayMsgRecordDto dto){
        PartyPersonBirthdayMsgRecordEntity entity = CopyUtils.copyObj(dto,PartyPersonBirthdayMsgRecordEntity.class);

        partyPersonBirthdayMsgRecordDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PartyPersonBirthdayMsgRecordDto dto) {
       PartyPersonBirthdayMsgRecordEntity entity = CopyUtils.copyObj(dto,PartyPersonBirthdayMsgRecordEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, partyPersonBirthdayMsgRecordDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        partyPersonBirthdayMsgRecordDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
